﻿#region File Description
//-----------------------------------------------------------------------------
// WaitEventTask
//
// Copyright © 2013 Wave Coorporation. All rights reserved.
// Use is subject to license terms.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Diagnostics;
using WaveEngine.Framework;
#endregion

namespace WaveEngine.Extensions.ITasks
{
    class ActiveWaitConditionTask : SceneBehavior, ITask
    {
        /// <summary>
        /// The token
        /// </summary>
        BasicTaskToken token;

        /// <summary>
        /// Gets or sets the predicate.
        /// </summary>
        /// <value>
        /// The predicate.
        /// </value>
        public Func<bool> Predicate { get; set; }

        /// <summary>
        /// Gets the scene.
        /// </summary>
        /// <value>
        /// The scene that owns this task. If it is null it can be considered a global task.
        /// </value>
        /// <exception cref="System.NotImplementedException"></exception>
        public new Scene Scene
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the name of the task.
        /// </summary>
        /// <value>
        /// The name of the task.
        /// </value>
        public string TaskName
        {
            get;

            private set;
        }

        /// <summary>
        /// Gets the child tasks.
        /// </summary>
        /// <value>
        /// The child tasks.
        /// </value>
        public IEnumerable<ITask> ChildTasks
        {
            get
            {
                return System.Linq.Enumerable.Empty<ITask>();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActiveWaitConditionTask" /> class.
        /// </summary>
        /// <param name="scene">The scene.</param>
        public ActiveWaitConditionTask(Scene scene, Func<bool> BreakPredicate, int EventCount = 1)
        {
            token = new BasicTaskToken(scene,"WaitConditionTask"+Guid.NewGuid().ToString());
            this.Scene = scene;
            this.Predicate = BreakPredicate;
        }

        /// <summary>
        /// Gets the state of the task.
        /// </summary>
        /// <value>
        /// The state of the task.
        /// </value>
        /// <exception cref="System.NotImplementedException"></exception>
        public WaveEngine.Framework.Services.TaskState TaskState
        {
            get { return token.TaskState; }
        }

        /// <summary>
        /// Occurs when [completed].
        /// </summary>
        public event Action Completed
        {
            add { this.token.Completed += value; }
            remove { this.token.Completed -= value; }
        }

        /// <summary>
        /// Occurs when [cancelled].
        /// </summary>
        public event Action Cancelled
        {
            add { this.token.Cancelled += value; }
            remove { this.token.Cancelled -= value; }
        }

        /// <summary>
        /// Occurs when [skipped] the task is skipped externally.
        /// </summary>
        public event Action Skipped
        {
            add { this.token.Skipped += value; }
            remove { this.token.Skipped -= value; }
        }

        /// <summary>
        /// Cancels this instance.
        /// </summary>
        public void Cancel()
        {
            this.token.Cancel();
            this.Scene.RemoveSceneBehavior(this);
        }

        /// <summary>
        /// The skip method is used when it is required to stop the task inmediatelly (or at least soon as possible)  in
        /// a coherent state. Conversely to the Cancel method, the skip method continues the workflow (the Completed event is called) and also
        /// calls to the Skipped Event.
        /// </summary>
        public bool TrySkip()
        {
            return this.token.TrySkip();
        }

        /// <summary>
        /// It starts the workflow that contains this task.
        /// </summary>
        public void Run()
        {
            this.Scene.AddSceneBehavior(this, Order.PostUpdate);
            this.token.Run();
        }

        /// <summary>
        /// Allows this instance to execute custom logic during its <c>Update</c>.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// This method will not be executed if the <see cref="T:WaveEngine.Framework.Component" />, or the <see cref="T:WaveEngine.Framework.Entity" />
        /// owning it are not <c>Active</c>.
        /// </remarks>
        [DebuggerStepThrough]
        protected override void Update(TimeSpan gameTime)
        {
            if (this.TaskState == WaveEngine.Framework.Services.TaskState.Running && this.Predicate())
                Terminate();
        }

        /// <summary>
        /// Terminates this instance.
        /// </summary>
        [DebuggerStepThrough]
        private void Terminate()
        {
            this.token.NotifyTaskCompleted();
            this.Scene.RemoveSceneBehavior(this);
        }

        /// <summary>
        /// Resolves the dependencies needed for this instance to work.
        /// </summary>
        /// <exception cref="System.NotImplementedException"></exception>
        protected override void ResolveDependencies()
        {

        }
    }
}
